<?php

$basedir = dirname(__FILE__);

require_once $basedir . '/../../../classes/atom.php';
require_once $basedir . '/../../../classes/atom/childelement.php';
require_once $basedir . '/../../../classes/atom/entry.php';

require_once $basedir . '/../../../classes/atom/entry/id.php';
require_once $basedir . '/../../../classes/atom/entry/content.php';
require_once $basedir . '/../../../classes/atom/entry/title.php';
require_once $basedir . '/../../../classes/atom/entry/updated.php';
require_once $basedir . '/../../../classes/atom/entry/link.php';
require_once $basedir . '/../../../classes/atom/entry/link/avatar.php';
require_once $basedir . '/../../../classes/atom/entry/link/alternatehtml.php';

require_once $basedir . '/../../../classes/atom/entry/author.php';
require_once $basedir . '/../../../classes/atom/entry/author/uri.php';
require_once $basedir . '/../../../classes/atom/entry/author/name.php';
require_once $basedir . '/../../../classes/atom/entry/author/link/photo.php';

require_once $basedir . '/../../../classes/activitystream.php';
require_once $basedir . '/../../../classes/activitystream/actor.php';
require_once $basedir . '/../../../classes/activitystream/objecttype.php';
require_once $basedir . '/../../../classes/activitystream/object.php';
require_once $basedir . '/../../../classes/activitystream/verb.php';

require_once $basedir . '/../../../classes/thr.php';
require_once $basedir . '/../../../classes/thr/inreplyto.php';

class Atom_EntryTest extends PHPUnit_Framework_TestCase {
    
    public function testConstructorOfEntryObject() {
        $object = new Atom_Entry();
        $this->assertEquals("Atom_Entry", get_class($object));
    }
    
    public function testCreationOfFullAtomEntry() {
        date_default_timezone_set('Europe/Berlin');
        
        // Let's create the needed elements.
        $id = new Atom_Entry_Id("mimime:12345689");
		$title = new Atom_Entry_Title("Tuomas is now following Pamela Anderson &é\"'(-è_çà)='");
		$content = new Atom_Entry_Content("Tuomas is now following Pamela Anderson Tuomas is now following Pamela Anderson &é\"'(-è_çà)='", 
		                                  Atom_Entry_Content::TYPE_TEXTHTML);
		$updated = new Atom_Entry_Updated('2010-07-26T06:42:55+02:00');
		
		$author = new Atom_Entry_Author();
		$author->addChildElement(new Atom_Entry_Author_Uri('http://lobstermonster.org/tuomas'));
		$author->addChildElement(new Atom_Entry_Author_Name("Tuomas Koski Tuomas is now following Pamela Anderson &é\"'(-è_çà)='"));
		
		// Let's create the activity actor
		$as_actor = new Activitystream_Actor();
		$as_actor->addChildElement(new Activitystream_ObjectType(Activitystream_ObjectType::TYPE_PERSON));
		
		$as_actor->addChildElement(new Atom_Entry_Id("tuomas@lobstermonster.org Tuomas is now following Pamela Anderson &é\"'(-è_çà)='"));
		$as_actor->addChildElement(new Atom_Entry_Title("Tuomas Koski Tuomas is now following Pamela Anderson &é\"'(-è_çà)='"));
		$as_actor->addChildElement(new Atom_Entry_Link(Atom_Entry_Link::REL_ALTERNATE, 
		                                               Atom_Entry_Link::TYPE_TEXTHTML, 
		                                               'http://identi.ca/tkoski'));
		
        // And let's create the activity verb
        $as_verb = new Activitystream_Verb(Activitystream_Verb::VERB_FOLLOW);

        // Finally let's create the Atom Entry itself
		$atom = new Atom_Entry();
		
		$atom->addChildElement($id);
		$atom->addChildElement($title);
		$atom->addChildElement($content);
		$atom->addChildElement($updated);
		$atom->addChildElement($author);
		$atom->addChildElement($as_actor);
		$atom->addChildElement($as_verb);
		
		// Let's add needed new namespaces.
		$atom->addNamespace(ActivityStream::AS_NS_PREFIX, Activitystream::AS_NS);
		
		$expected = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
  <id>mimime:12345689</id>
  <title>Tuomas is now following Pamela Anderson &amp;é"\'(-è_çà)=\'</title>
  <content type="text/html">Tuomas is now following Pamela Anderson Tuomas is now following Pamela Anderson &amp;é"\'(-è_çà)=\'</content>
  <updated>2010-07-26T06:42:55+02:00</updated>
  <author>
    <uri>http://lobstermonster.org/tuomas</uri>
    <name>Tuomas Koski Tuomas is now following Pamela Anderson &amp;é"\'(-è_çà)=\'</name>
  </author>
  <activity:actor>
    <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
    <id>tuomas@lobstermonster.org Tuomas is now following Pamela Anderson &amp;é"\'(-è_çà)=\'</id>
    <title>Tuomas Koski Tuomas is now following Pamela Anderson &amp;é"\'(-è_çà)=\'</title>
    <link rel="alternate" type="text/html" href="http://identi.ca/tkoski"/>
  </activity:actor>
  <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
</entry>
';
		$this->assertEquals($expected, $atom->asXML());
	
    }
    
    public function testParsingOfSalmonFollowEntry() {
        $rawXML = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/">
  <id>mimime:1280844851</id>
  <title>User C is now following user-b@localhost</title>
  <content type="html">&lt;a href=\'http://localhost/index.php?controller=profile&amp;username=user-c\'&gt;User C is now following user-b@localhost</content>
  <author>
    <uri>acct:user-c@localhost</uri>
    <name>User C</name>
  </author>
  <activity:actor>
    <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
    <id>http://localhost/index.php?controller=profile&amp;username=user-c</id>
    <title>User C</title>
    <link rel="alternate" type="text/html" href="http://localhost/index.php?controller=profile&amp;username=user-c"/>
  </activity:actor>
  <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
</entry>';
        
        $atom = Atom_Entry::buildFromRawXML($rawXML);
        $this->assertEquals('Atom_Entry_Author', get_class($atom->getAuthor()));
        $this->assertEquals('User C', $atom->getAuthor()->getName()->getName());
        $this->assertEquals('acct:user-c@localhost', $atom->getAuthor()->getUri()->getUri());
        $this->assertEquals('http://activitystrea.ms/schema/1.0/follow', $atom->getActivitystreamVerb()->getVerb());
        $this->assertEquals('User C is now following user-b@localhost', $atom->getTitle()->getTitle());
        
    }
    
    public function testParsingSalmonUnfollowEntryFromIdentica() {
        $rawXML = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:georss="http://www.georss.org/georss" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:media="http://purl.org/syndication/atommedia">
 <id>tag:identi.ca,2010-08-10:unfollow:52467:227750:1970-01-01T00:00:00+00:00</id>
 <title>Unfollow</title>
 <published>1970-01-01T00:00:00+00:00</published>
 <content type="html">Tuomas Koski stopped following Tuomas Koski.</content>
 <author>
  <uri>http://identi.ca/user/52467</uri>
  <name>Tuomas Koski</name>
</author>
<activity:actor>
 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
 <id>http://identi.ca/user/52467</id>
 <title>Tuomas Koski</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/tkoski"/>
 <link rel="avatar" type="image/png" media:width="96" media:height="96" href="http://theme.status.net/0.9.3/identica/default-avatar-profile.png"/>
 <link rel="avatar" type="image/png" media:width="48" media:height="48" href="http://theme.status.net/0.9.3/identica/default-avatar-stream.png"/>
 <link rel="avatar" type="image/png" media:width="24" media:height="24" href="http://theme.status.net/0.9.3/identica/default-avatar-mini.png"/>
<poco:preferredUsername>tkoski</poco:preferredUsername>
<poco:displayName>Tuomas Koski</poco:displayName>
<poco:note>Happy Finnish programmer.</poco:note>
<poco:address>
 <poco:formatted>Paris</poco:formatted>
</poco:address>
<poco:urls>
 <poco:type>homepage</poco:type>
 <poco:value>http://www.lobstermonster.org</poco:value>
 <poco:primary>true</poco:primary>
</poco:urls>
</activity:actor>
 <activity:verb>http://ostatus.org/schema/1.0/unfollow</activity:verb>
</entry>
';
        $atom = Atom_Entry::buildFromRawXML($rawXML);
        $this->assertEquals('http://identi.ca/user/52467', $atom->getAuthor()->getUri()->getUri());
    }
    
    public function testParsingOfSalmonComment() {
        $rawXML = '<?xml version="1.0" encoding="UTF-8"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">
 <source>
  <id>http://identi.ca/mairin</id>
  <title>Máirín - Identi.ca</title>
  <link href="http://identi.ca/mairin"/>
  <link rel="self" type="application/atom+xml" href="http://identi.ca/mairin"/>
  <link rel="license" href="http://creativecommons.org/licenses/by/3.0/"/>
  <icon>http://avatar.identi.ca/3081-96-20100429190851.jpeg</icon>
  <updated>2010-08-06T14:40:22+00:00</updated>
</source>
 <title>@koski both!</title>
<author>
 <name>mairin</name>
 <uri>http://identi.ca/user/3081</uri>
</author>
<activity:actor>
 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
 <id>http://identi.ca/user/3081</id>
 <title>Máirín</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/mairin"/>
 <link rel="avatar" type="image/jpeg" media:width="480" media:height="480" href="http://avatar.identi.ca/3081-480-20100429190851.jpeg"/>
 <link rel="avatar" type="image/jpeg" media:width="96" media:height="96" href="http://avatar.identi.ca/3081-96-20100429190851.jpeg"/>
 <link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="http://avatar.identi.ca/3081-48-20100429190851.jpeg"/>
 <link rel="avatar" type="image/jpeg" media:width="24" media:height="24" href="http://avatar.identi.ca/3081-24-20100429190851.jpeg"/>
 <georss:point>42.3584308 -71.0597732</georss:point>
<poco:preferredUsername>mairin</poco:preferredUsername>
<poco:displayName>Máirín</poco:displayName>
<poco:note>I\'m an free / open source software interaction designer. I love Fedora and Hello Kitty among other things.</poco:note>
<poco:urls>
 <poco:type>homepage</poco:type>
 <poco:value>http://mairin.wordpress.com</poco:value>
 <poco:primary>true</poco:primary>
</poco:urls>
</activity:actor>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/45263127"/>
 <id>http://identi.ca/notice/45263127</id>
 <published>2010-08-06T14:40:22+00:00</published>
 <updated>2010-08-06T14:40:22+00:00</updated>
 <statusnet:notice_info local_id="45263127" source="pino"></statusnet:notice_info>
 <link rel="related" href="http://identi.ca/notice/45262687"/>
 <thr:in-reply-to ref="mimime:1281104680" href="http://identi.ca/notice/45262687"></thr:in-reply-to>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/44935926"/>
 <link rel="ostatus:attention" href="http://www.lobstermonster.org/profile/koski"/>
 <content type="html">@&lt;span class=&quot;vcard&quot;&gt;&lt;a href=&quot;http://www.lobstermonster.org/profile/koski&quot; class=&quot;url&quot; title=&quot;Tuomas Koski&quot;&gt;&lt;span class=&quot;fn nickname&quot;&gt;koski&lt;/span&gt;&lt;/a&gt;&lt;/span&gt; both!</content>
 <georss:point>42.3584308 -71.0597732</georss:point>
</entry>';
        
        $atom = Atom_Entry::buildFromRawXML($rawXML);
        $this->assertEquals('Atom_Entry_Author', get_class($atom->getAuthor()));
        $this->assertEquals('mairin', $atom->getAuthor()->getName()->getName());
        $this->assertEquals('mimime:1281104680', $atom->getInReplyTo()->getRef());

$excepted = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0">
  <id>http://identi.ca/mairin</id>
  <author>
    <name>mairin</name>
    <uri>http://identi.ca/user/3081</uri>
  </author>
  <link rel="alternate" type="text/html" href="http://identi.ca/notice/45263127"/>
  <updated>2010-08-06T14:40:22+00:00</updated>
  <title>Máirín - Identi.ca</title>
  <content type="html">@&lt;span class="vcard"&gt;&lt;a href="http://www.lobstermonster.org/profile/koski" class="url" title="Tuomas Koski"&gt;&lt;span class="fn nickname"&gt;koski&lt;/span&gt;&lt;/a&gt;&lt;/span&gt; both!</content>
  <activity:actor>
    <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
    <id>http://identi.ca/user/3081</id>
    <title>Máirín</title>
    <link rel="alternate" type="text/html" href="http://identi.ca/mairin"/>
  </activity:actor>
  <thr:in-reply-to ref="mimime:1281104680" href="http://identi.ca/notice/45262687"/>
</entry>
';
        $this->assertEquals($excepted, $atom->asXml());
    }

    public function testParsingOfSalmonCommentNoThrFound() {
        $rawXML = '<?xml version="1.0" encoding="UTF-8"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">
 <source>
  <id>http://identi.ca/mairin</id>
  <title>mairin - Identi.ca</title>
  <link href="http://identi.ca/mairin"/>
  <link rel="self" type="application/atom+xml" href="http://identi.ca/mairin"/>
  <link rel="license" href="http://creativecommons.org/licenses/by/3.0/"/>
  <icon>http://avatar.identi.ca/3081-96-20100429190851.jpeg</icon>
  <updated>2010-08-06T14:40:22+00:00</updated>
</source>
 <title>@koski both!</title>
<author>
 <name>mairin</name>
 <uri>http://identi.ca/user/3081</uri>
</author>
<activity:actor>
 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
 <id>http://identi.ca/user/3081</id>
 <title>Máirín</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/mairin"/>
 <link rel="avatar" type="image/jpeg" media:width="480" media:height="480" href="http://avatar.identi.ca/3081-480-20100429190851.jpeg"/>
 <link rel="avatar" type="image/jpeg" media:width="96" media:height="96" href="http://avatar.identi.ca/3081-96-20100429190851.jpeg"/>
 <link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="http://avatar.identi.ca/3081-48-20100429190851.jpeg"/>
 <link rel="avatar" type="image/jpeg" media:width="24" media:height="24" href="http://avatar.identi.ca/3081-24-20100429190851.jpeg"/>
 <georss:point>42.3584308 -71.0597732</georss:point>
<poco:preferredUsername>mairin</poco:preferredUsername>
<poco:displayName>Máirín</poco:displayName>
<poco:note>I\'m an free / open source software interaction designer. I love Fedora and Hello Kitty among other things.</poco:note>
<poco:urls>
 <poco:type>homepage</poco:type>
 <poco:value>http://mairin.wordpress.com</poco:value>
 <poco:primary>true</poco:primary>
</poco:urls>
</activity:actor>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/45263127"/>
 <id>http://identi.ca/notice/45263127</id>
 <published>2010-08-06T14:40:22+00:00</published>
 <updated>2010-08-06T14:40:22+00:00</updated>
 <statusnet:notice_info local_id="45263127" source="pino"></statusnet:notice_info>
 <link rel="related" href="http://identi.ca/notice/45262687"/>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/44935926"/>
 <link rel="ostatus:attention" href="http://www.lobstermonster.org/profile/koski"/>
 <content type="html">@&lt;span class=&quot;vcard&quot;&gt;&lt;a href=&quot;http://www.lobstermonster.org/profile/koski&quot; class=&quot;url&quot; title=&quot;Tuomas Koski&quot;&gt;&lt;span class=&quot;fn nickname&quot;&gt;koski&lt;/span&gt;&lt;/a&gt;&lt;/span&gt; both!</content>
 <georss:point>42.3584308 -71.0597732</georss:point>
</entry>';
        
        $atom = Atom_Entry::buildFromRawXML($rawXML);
        $this->assertEquals('Atom_Entry_Author', get_class($atom->getAuthor()));
        $this->assertEquals('mairin', $atom->getAuthor()->getName()->getName());
        $this->assertEquals(null, $atom->getInReplyTo());
    }
    
}